{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8bcfdcc0-07bc-4a0b-90dc-84444e5f65d5",
   "metadata": {},
   "source": [
    "# PyBaMM DataLoader\n",
    "\n",
    "This notebook is a reference for using pybamm.DataLoader module for using and fetching data files from the pybamm-data registry.\n",
    "Checkout the [documentation](../../api/pybamm_data.rst) for further implementation details on this module.\n",
    "\n",
    "The following steps provide an example for using pybamm.DataLoader to download data files from PyBaMM data registry upstream at [pybamm-data](https://github.com/pybamm-team/pybamm-data/releases).\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5050f302-5246-43ca-a3cd-a50570ad2983",
   "metadata": {},
   "source": [
    "### 1. Creating DataLoader instance and listing data files present in the registry."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c641a158-388a-4ebb-8be5-7f736c3be159",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "An NVIDIA GPU may be present on this machine, but a CUDA-enabled jaxlib is not installed. Falling back to cpu.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['comsol_01C.json',\n",
       " 'comsol_05C.json',\n",
       " 'comsol_1C.json',\n",
       " 'comsol_1plus1D_3C.json',\n",
       " 'comsol_2C.json',\n",
       " 'comsol_3C.json',\n",
       " 'Ecker_1C.csv',\n",
       " 'Ecker_5C.csv',\n",
       " '0.1C_discharge_U.txt',\n",
       " '0.1C_discharge_displacement.txt',\n",
       " '0.5C_discharge_T.txt',\n",
       " '0.5C_discharge_U.txt',\n",
       " '0.5C_discharge_displacement.txt',\n",
       " '1C_discharge_T.txt',\n",
       " '1C_discharge_U.txt',\n",
       " '1C_discharge_displacement.txt',\n",
       " '2C_discharge_T.txt',\n",
       " '2C_discharge_U.txt',\n",
       " '2C_discharge_displacement.txt',\n",
       " 'stn_2C.txt',\n",
       " 'stp_2C.txt',\n",
       " 'UDDS.csv',\n",
       " 'US06.csv',\n",
       " 'WLTC.csv',\n",
       " 'car_current.csv']"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%pip install \"pybamm[plot,cite]\" -q    # install PyBaMM if it is not installed\n",
    "import pybamm\n",
    "\n",
    "data_loader = pybamm.DataLoader()\n",
    "data_loader.show_registry()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2bf77f88-3ab0-475a-84e0-cd0b8d3028a4",
   "metadata": {},
   "source": [
    "### 2. Listing data files along with their checksums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "50addccb-a1d0-48e0-9510-6697702f7eaa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'comsol_01C.json': 'sha256:bc5136fe961e269453bdc31fcaa97376d6f8c347d570fd30ce4b7660c68ae22c',\n",
       " 'comsol_05C.json': 'sha256:3b044135ad88bdb88959304a33fe42b654d5ef7ef79d1271dd909cec55b257fb',\n",
       " 'comsol_1C.json': 'sha256:d45e3ab482c497c37ebbc68898da22bab0b0263992d8f2302502028bfd5ba0e9',\n",
       " 'comsol_1plus1D_3C.json': 'sha256:cdd5759202f9c7887d2ea6032f82212f2ca89297191fe5282b8812e1a09b1e1f',\n",
       " 'comsol_2C.json': 'sha256:15c2637f54bf1639621c58795db859cb08611c8182b7b20ade10e4c3e2839a5b',\n",
       " 'comsol_3C.json': 'sha256:11d5afccb70be85d4ac7e61d413c6e0f5e318e1635b1347c9a3c6784119711e6',\n",
       " 'Ecker_1C.csv': 'sha256:428dc5113a6430492f430fb9e895f67d3e20f5643dc49a1cc0a922b92a5a8e01',\n",
       " 'Ecker_5C.csv': 'sha256:a89f8bf6e305b2a4195e1fae5e803277a40ed7557d263ef726f621803dcbb495',\n",
       " '0.1C_discharge_U.txt': 'sha256:7b9fcd137441eea4ab686faee8d57fe242c5544400939ef358ccd99c63c9579d',\n",
       " '0.1C_discharge_displacement.txt': 'sha256:f1329731ead5a82a2be9851cf80e4c6d68dd0774e07aee5361e2af3ab420d7be',\n",
       " '0.5C_discharge_T.txt': 'sha256:2140b2f6bd698135d09a25b1f04c271d35a3a02999ace118b10389e01defa2ae',\n",
       " '0.5C_discharge_U.txt': 'sha256:9ed8368b2c6149d2a69218e7df6aaade2511c9f7f6fc7932cda153d9a3a10f39',\n",
       " '0.5C_discharge_displacement.txt': 'sha256:8098565ff99bc938864797b402f483c1c64a583d6db85d086f39ab0e7b638dd1',\n",
       " '1C_discharge_T.txt': 'sha256:97308dfd7f7dd6c434e30f6c00fb6707c43c963855bb0800e0336809d5cc3756',\n",
       " '1C_discharge_U.txt': 'sha256:8fc19de45172215d65c56522c224e6fc700ee443db236b814238a829b7a14c3a',\n",
       " '1C_discharge_displacement.txt': 'sha256:c2e8617ac48a20921da1b40bbebac479a0a143edf16b12b2e1ff9aaaf1a32ff4',\n",
       " '2C_discharge_T.txt': 'sha256:4bd688fb7653539701fe3df61857474b4d54e8b142c84fdc4c8b92b9573fa5d0',\n",
       " '2C_discharge_U.txt': 'sha256:7b3c24b5e6df377075002abc2f62bab7c88b27d826812ba5a4c8385a1a12e723',\n",
       " '2C_discharge_displacement.txt': 'sha256:2b11513d80827c762325c819a084b87b3a239af7d112f234c9871481760a0013',\n",
       " 'stn_2C.txt': 'sha256:bb2f90ccfd2cd86ad589287caae13470e554df2f4f47f0f583a5a7e3e6bd9d4c',\n",
       " 'stp_2C.txt': 'sha256:6fe73b3a18e5fcfb95151dfd7d34c3cbe929792631447ed3ec88c047c9778223',\n",
       " 'UDDS.csv': 'sha256:9fe6558c17aad3cc08109186923aeb7459cd3097a381c44e854bf22dd12a5a2a',\n",
       " 'US06.csv': 'sha256:5909eb2ec7983fae86a050ff3b35a2041d0ab698710a6b0f95d5816e348077ba',\n",
       " 'WLTC.csv': 'sha256:bb2f95018a44ac1425cb9c787c34721192af502c7385f1358f28e4f75df11fd8',\n",
       " 'car_current.csv': 'sha256:4305b91b9df073cb048c25dd3fae725e06a94fe200e322e5c08db290d6799e36'}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_loader.files"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "47c826d1-37ed-4d9f-95f1-c919902f04d6",
   "metadata": {},
   "source": [
    "### 3. Fetching a file from upstream and storing it in local cache folder"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "757aa66c-b8f3-4aa9-86af-8c9355bb7be1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PosixPath('/home/santa/.cache/pybamm/v1.0.0/Ecker_1C.csv')"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_loader.get_data(\"Ecker_1C.csv\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8d667cce-43ab-4079-ac16-2b8969294690",
   "metadata": {},
   "source": [
    "### 4. Loading a file from cache into python code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6661ce52-1fbe-4546-a00a-8f96c3dca95c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>20.3084233101775</th>\n",
       "      <th>4.10984760218981</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>137.255118</td>\n",
       "      <td>4.061710</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>247.454889</td>\n",
       "      <td>4.020866</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>393.638258</td>\n",
       "      <td>3.981480</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>530.825727</td>\n",
       "      <td>3.940636</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>670.262171</td>\n",
       "      <td>3.911462</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>820.943489</td>\n",
       "      <td>3.874994</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>955.881984</td>\n",
       "      <td>3.845819</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>1097.567403</td>\n",
       "      <td>3.812269</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>1243.750772</td>\n",
       "      <td>3.790388</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>1383.187216</td>\n",
       "      <td>3.761213</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>1529.370585</td>\n",
       "      <td>3.736415</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>1666.558054</td>\n",
       "      <td>3.724745</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>1808.243473</td>\n",
       "      <td>3.705782</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>1947.679917</td>\n",
       "      <td>3.698488</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>2087.116361</td>\n",
       "      <td>3.688277</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>2231.050755</td>\n",
       "      <td>3.675149</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>2379.483099</td>\n",
       "      <td>3.660562</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>2521.168518</td>\n",
       "      <td>3.635763</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>2658.355987</td>\n",
       "      <td>3.600754</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>2802.290381</td>\n",
       "      <td>3.554075</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>2939.477850</td>\n",
       "      <td>3.510313</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>3081.163269</td>\n",
       "      <td>3.473845</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>3225.097663</td>\n",
       "      <td>3.430084</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>3344.293333</td>\n",
       "      <td>3.381946</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>3470.235928</td>\n",
       "      <td>3.307551</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>3553.447999</td>\n",
       "      <td>3.212734</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>3616.419296</td>\n",
       "      <td>3.115000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>3652.402895</td>\n",
       "      <td>3.018724</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>3688.386493</td>\n",
       "      <td>2.899109</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>3715.374192</td>\n",
       "      <td>2.766366</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    20.3084233101775  4.10984760218981\n",
       "0         137.255118          4.061710\n",
       "1         247.454889          4.020866\n",
       "2         393.638258          3.981480\n",
       "3         530.825727          3.940636\n",
       "4         670.262171          3.911462\n",
       "5         820.943489          3.874994\n",
       "6         955.881984          3.845819\n",
       "7        1097.567403          3.812269\n",
       "8        1243.750772          3.790388\n",
       "9        1383.187216          3.761213\n",
       "10       1529.370585          3.736415\n",
       "11       1666.558054          3.724745\n",
       "12       1808.243473          3.705782\n",
       "13       1947.679917          3.698488\n",
       "14       2087.116361          3.688277\n",
       "15       2231.050755          3.675149\n",
       "16       2379.483099          3.660562\n",
       "17       2521.168518          3.635763\n",
       "18       2658.355987          3.600754\n",
       "19       2802.290381          3.554075\n",
       "20       2939.477850          3.510313\n",
       "21       3081.163269          3.473845\n",
       "22       3225.097663          3.430084\n",
       "23       3344.293333          3.381946\n",
       "24       3470.235928          3.307551\n",
       "25       3553.447999          3.212734\n",
       "26       3616.419296          3.115000\n",
       "27       3652.402895          3.018724\n",
       "28       3688.386493          2.899109\n",
       "29       3715.374192          2.766366"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "pd.read_csv(data_loader.get_data(\"Ecker_1C.csv\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6737e5a9-714a-48eb-a51a-cc41aaa9c231",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
